<?php

namespace App\Providers;

use App\Api\Store\Services\UserPointService;
use App\Libs\Top\TopLibrary;
use App\User;
use Carbon\Carbon;
use Illuminate\Support\ServiceProvider;
use Validator;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Validator::extend('mobile', function ($attribute, $value, $parameters, $validator) {
            $reg = "/^1[3456789][0-9]{9}$/";
            return preg_match($reg, $value);
        });

        Validator::replacer('mobile', function ($message, $attribute, $rule, $parameters) {
            return str_replace('validation.mobile', '手机号码不正确', $message);
        });

        Validator::extend('idNo', function ($attribute, $value, $parameters, $validator) {
            return isIdNo($value);
        });

        Validator::replacer('idNo', function ($message, $attribute, $rule, $parameters) {
            return str_replace('validation.id_no', '身份证号码不正确', $message);
        });

        User::creating(function ($user) {
            $user->api_token = $user->api_token ?: str_random(48);
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton('top', function ($app) {
            return new TopLibrary($app);
        });
        $this->app->singleton('dingRobot', function ($app) {
            return new \App\Libs\DingBot\DingBot($app);
        });
        $this->app->singleton('rockyPay', function ($app) {
            return new \App\Libs\Payment\RockyPayLibrary($app);
        });
        $this->app->singleton('UserPoint', function ($app) {
            return new UserPointService($app);
        });
        $this->app->singleton('gps', function ($app) {
            return new UserPointService($app);
        });
        if ($this->app->environment() == 'dev') {
            $this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
        }
    }
}
